package com.carsa.blackberry.domain.dto;

import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.util.DateUtil;
import com.carsa.blackberry.util.XMLHandler;
import com.carsa.blackberry.util.persistence.Persistable;

/**
 * @author Nahuel Barrios.
 * @since 23/08/2010.
 * @version 1.0
 */
public class DriversQueueDTO implements Persistable {

    private static final String XML_ELEMENT_ROOT_TAG    = "data";
    private static final String XML_ELEMENT_LAST_UPDATE = "lastUpdate";
    private static final String XML_ELEMENT_DRIVERS     = "drivers";

    private Date                _lastUpdate;
    private Vector              _drivers                = new Vector();

    public DriversQueueDTO(String xmlSource) {
	super();
	this.initializeFromXML(xmlSource);
    }

    public DriversQueueDTO() {
	super();
    }

    public String toString() {
	return "DriversQueueDTO [drivers=" + _drivers + ", lastUpdate=" + _lastUpdate + "]";
    }

    /**
     * @param xmlSource
     *            El {@link String} en formato XML del cual se deben recuperar los atributos para setear el objeto this.
     */
    public int initializeFromXML(String xmlSource) {
	XMLHandler xml = new XMLHandler(xmlSource);
	_lastUpdate = DateUtil.getFromString(xml.getElementValue(XML_ELEMENT_LAST_UPDATE), DateUtil.DATE_SEPARATOR_BAR);

	xml = new XMLHandler(xml.getElementValue(XML_ELEMENT_DRIVERS));
	int elementsCount = xml.getElementCount();

	int idx = 0;
	for (idx = 0; idx < elementsCount; idx++) {
	    _drivers.addElement(new DriverDTO(xml.getFirstElementValueOverIteration()));
	}

	return idx;
    }

    public Vector getOnlyDrivers() {
	Vector ret = new Vector();

	Enumeration drivers = _drivers.elements();
	while (drivers.hasMoreElements()) {
	    DriverDTO aDriver = (DriverDTO) drivers.nextElement();

	    if (aDriver.isDriver())
		ret.addElement(aDriver);
	}

	return ret;
    }

    public Vector getOnlyAyudantes() {
	Vector ret = new Vector();

	Enumeration drivers = _drivers.elements();
	while (drivers.hasMoreElements()) {
	    DriverDTO aDriver = (DriverDTO) drivers.nextElement();

	    if (aDriver.isAyudante())
		ret.addElement(aDriver);
	}

	return ret;
    }

    /**
     * @return the _lastUpdate
     */
    public Date getLastUpdate() {
	return _lastUpdate;
    }

    /**
     * @param lastUpdate
     *            the lastUpdate to set
     */
    public void setLastUpdate(Date lastUpdate) {
	_lastUpdate = lastUpdate;
    }

    /**
     * @return the drivers
     */
    public Vector getDrivers() {
	return _drivers;
    }

    /**
     * @param drivers
     *            the drivers to set
     */
    public void setDrivers(Vector drivers) {
	_drivers = drivers;
    }

    public String getPersistPath() {
	return MainKeys.FILE_PATH_DRIVERS;
    }

    public String getStringToPersist() {
	XMLHandler xml = new XMLHandler(XML_ELEMENT_ROOT_TAG, null, null);

	xml.addElementAtRoot(XML_ELEMENT_LAST_UPDATE, null, DateUtil.exportDateAs(Calendar.getInstance().getTime(),
	        DateUtil.DATE_ORDER_DD_MM_AAAA, DateUtil.DATE_SEPARATOR_BAR));

	Enumeration drivers = _drivers.elements();
	XMLHandler xmlDrivers = new XMLHandler(XML_ELEMENT_DRIVERS, null, null);
	while (drivers.hasMoreElements()) {
	    DriverDTO aDriver = (DriverDTO) drivers.nextElement();

	    xmlDrivers.addElementAtRoot(aDriver.toXMLString());
	}
	xml.addElementAtRoot(xmlDrivers.toXMLString());

	return MainKeys.XML_VERSION_TAG + xml.toString();
    }

    public Vector getDriversFromSameZone(String legajoAyudante) {
	Vector ret= new Vector();
	
	DriverDTO aDriver= this.get(legajoAyudante);
	
	if(aDriver==null)
	    return ret;
	
	Enumeration drivers= this.getOnlyDrivers().elements();
	while (drivers.hasMoreElements()) {
	    DriverDTO eachDriver = (DriverDTO) drivers.nextElement();
	    if(eachDriver.getZone().equals(aDriver.getZone()))
		ret.addElement(eachDriver);
        }
	
	return ret;
    }

    private DriverDTO get(String legajoAyudante) {
	Enumeration drivers= this._drivers.elements();
	while (drivers.hasMoreElements()) {
	    DriverDTO aDriver = (DriverDTO) drivers.nextElement();
	    if(aDriver.getLegajo().equals(legajoAyudante))
		return aDriver;
        }
	return null;
    }

}
